<?php

namespace app\utils;

use ReflectionClass;
use think\facade\Config;
use think\facade\Log;
use think\Request;
use ZipArchive;

//use ZipArchive;

class FileUtills
{
    /**
     * @desc
     * @params $fileArray
     * $fileArray = [
     * ["fileName" => "1.vue", "fileContent" => '我是你爸爸',
     * ]
     * ];
     */
    static function zipFileArray($fileArray)
    {
        if (empty($fileArray)) {
            return ["success" => false, 'message' => "文件数组是空",
            ];
        }
        $dicName = Utils::fdate(time(), '/y/m/d/');
        $dicPath = Config('filesystem.disks.public.root') . $dicName;
        if (!file_exists($dicPath)) {
            mkdir($dicPath);
        }
        $zipFileName = Md5::createTokenStr();
        $zipPath = $dicPath . "$zipFileName.zip";
        Log::info("zip的路径是,$zipPath");
        $webUrl = Config('filesystem.disks.public.url') . $dicName . "$zipFileName.zip";
        $zip = new ZipArchive();
        /*默认打开文件 打不开默认新增zip文件*/
        if ($zip->open($zipPath, ZIPARCHIVE::CREATE) !== TRUE) {
//        exit("文件打开失败!");
            return ['success' => false, 'message' => '文件打不开'];
        }

        foreach ($fileArray as $item) {
            $zip->addFromString($item['fileName'], $item['fileContent']);
        }
        $zip->close();
        return [
            "success" => true,
            "data" => $webUrl,
        ];
    }


}



